package com.ruoyi.reception;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.receipt.domain.TbExampleSubjectManager;
import com.ruoyi.receipt.service.ITbExampleSubjectManagerService;
import com.ruoyi.receipt.utils.StatisAnalysisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据预热Controller
 * 
 * @author ruoyi
 * @date 2021-04-02
 */
@RestController
@RequestMapping("/reception/preheatData")
public class PreheatDataController extends BaseController
{

    @Autowired
    private ITbExampleSubjectManagerService tbExampleSubjectManagerService;

    @Autowired
    private StatisAnalysisUtils statisAnalysisUtils;

    /**
     * 进行分页查询,并导入到数据库中
     * 预热题簇和例题关系到redis中
     */
    @PostMapping("/initClusteSubjecRelation")
    public AjaxResult initClusteSubjecRelation() {

        Integer pageSize=10;

        //查询出所有数据库中所有数据条数
        Integer allDataSize=tbExampleSubjectManagerService.selectCountAllSubjecttManager();

        //统计出一共能分几页
        Integer allPageNum=allDataSize%pageSize==0?allDataSize%pageSize:(allDataSize%pageSize)+1;

        java.util.Random random=new java.util.Random();

        for (int i = 0; i < allDataSize; i++) {
            TbExampleSubjectManager tbExampleSubjectManager=new TbExampleSubjectManager();
            tbExampleSubjectManager.setIsDel(0);

            //pagehelper默认从第1页开始
            PageHelper.startPage(i+1,pageSize);
            List<TbExampleSubjectManager> resultDateList = tbExampleSubjectManagerService.selectTbExampleSubjectManagerList(tbExampleSubjectManager);

            for (int j = 0; j < resultDateList.size(); j++) {
                TbExampleSubjectManager res=resultDateList.get(i);
                statisAnalysisUtils.putClusteSubjecRelation(res.getSubjectClusteId(),res.getId(),Double.parseDouble(random.nextInt(100)+""));
            }

        }

        return AjaxResult.success();
    }

}
